package heima.lm.oschina.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.bean.News;
import heima.lm.oschina.constant.Constants;
import heima.lm.oschina.utils.SpUtil;
import heima.lm.oschina.utils.StringUtils;
import heima.lm.oschina.utils.UIUtils;

/**
 * Created by herenxue on 2017/4/5.
 * 资讯的详情
 */

public class NewRvAdapter extends BaseRecyclerViewAdapter {

    public NewRvAdapter(List list) {
        super(list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(BaseApplication.mApp).inflate(R.layout.item_rv_news, parent, false);
        return new NewsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        final NewsViewHolder viewHolder = (NewsViewHolder) holder;
        final News news = (News) mList.get(position);
        String ids = SpUtil.getString(Constants.NEWS_IDS, "");
        if (ids.contains(String.valueOf(news.getId()))) {
            viewHolder.mTvNewsTitle.setTextColor(UIUtils.getColor(R.color.gray));
            viewHolder.mTvNewsContent.setTextColor(UIUtils.getColor(R.color.gray));
        } else {
            viewHolder.mTvNewsTitle.setTextColor(UIUtils.getColor(R.color.text_dark));
            viewHolder.mTvNewsContent.setTextColor(UIUtils.getColor(R.color.text_dark));
        }
        viewHolder.mTvNewsTitle.setText(news.getTitle());
        viewHolder.mTvNewsContent.setText(news.getBody());
        viewHolder.mTvNewsContent.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                copyToClipBoard(news.getBody());
                return false;
            }
        });
        viewHolder.mTvNewsAuthor.setText(news.getAuthor());
        viewHolder.mTvNewsDate.setText(StringUtils.getSubString(0, 10, news.getPubDate()));
        viewHolder.mTvNewsCommentCount.setText(String.valueOf(news.getCommentCount()));
    }

    private void copyToClipBoard(CharSequence text) {
        ClipboardManager cm = (ClipboardManager) BaseApplication.mApp.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(text);
        Toast.makeText(BaseApplication.mApp, "已复制到剪贴板", Toast.LENGTH_SHORT).show();
    }

    public static class NewsViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_news_title)
        TextView mTvNewsTitle;
        @BindView(R.id.tv_news_content)
        TextView mTvNewsContent;
        @BindView(R.id.tv_news_author)
        TextView mTvNewsAuthor;
        @BindView(R.id.tv_news_date)
        TextView mTvNewsDate;
        @BindView(R.id.tv_news_comment_count)
        TextView mTvNewsCommentCount;

        public NewsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
